package com.wxj.educenter.controller;


import com.wxj.commonutils.ordervo.UcenterMemberOrder;
import com.wxj.commonutils.result.R;
import com.wxj.commonutils.util.JwtUtils;
import com.wxj.educenter.entity.UcenterMember;
import com.wxj.educenter.entity.vo.ChangeVo;
import com.wxj.educenter.entity.vo.RegisterVo;
import com.wxj.educenter.service.UcenterMemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author wxj
 * @since 2021-03-13
 */
@Api(description = "登录和注册")
@RestController
@RequestMapping("/educenter/member")
//@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    /**
     * 1 登录
     *
     * @param member
     * @return
     */
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.success().data("token", token).message("登陆成功！");
    }

    /**
     * 2 注册
     *
     * @param registerVo
     * @return
     */
    @PostMapping("register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        memberService.register(registerVo);
        return R.success().message("注册成功！");
    }

    /**
     * 3 根据token获取用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = memberService.getById(memberId);
        return R.success().data("userInfo", member);
    }

    /**
     * 4 根据用户id获取用户信息
     *
     * @param id
     * @return
     */
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    /**
     * 5 查询某一天注册人数
     *
     * @param day
     * @return
     */
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = memberService.countRegisterDay(day);
        return R.success().data("countRegister", count);
    }

    /**
     * 6 更改密码
     *
     * @param changeVo
     * @return
     */
    @PostMapping("change")
    public R changePassword(@RequestBody ChangeVo changeVo) {
        memberService.changePasswd(changeVo);
        return R.success().message("修改密码成功");
    }

    /**
     * 7 根据用户id获取用户信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据用户id获取用户信息 个人中心用")
    @PostMapping("getUserInfo/{id}")
    public R getUserInfo(@PathVariable String id) {
        UcenterMember member = memberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member, ucenterMemberOrder);
        return R.success().data("memberInfo", ucenterMemberOrder);
    }

    /**
     * 8 用户信息修改功能
     *
     * @param ucenterMember
     * @return
     */
    @ApiOperation(value = "用户信息修改")
    @PostMapping("updateMember")
    public R updateMember(@RequestBody UcenterMember ucenterMember) {
        boolean flag = memberService.updateById(ucenterMember);
        if (flag) {
            return R.success();
        } else {
            return R.error();
        }
    }

}

